﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FoneBuddy.LinqData;
using FoneBuddy.LinqModel.Data;
using FoneBuddy.LinqModel;

public partial class TransactionCompleted : System.Web.UI.Page
{
    IDataRepository _dataObject = new DataRepository();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            CheckStatus();
    }

    private void CheckStatus()
    {
        try
        {
            string transactionId = Request.QueryString["payzippy_transaction_id"].ToString();
            if (transactionId != null)
            {
                string auth = string.Empty;
                bool IsSuccess = false;
                Int32 Id = 0;
                if (!string.IsNullOrEmpty(Request.QueryString["merchant_transaction_id"]))
                    Id = Convert.ToInt32(Request.QueryString["merchant_transaction_id"].ToString());

                if (!string.IsNullOrEmpty(Request.QueryString["transaction_response_code"]))
                    auth = Request.QueryString["transaction_response_code"].ToString();
                if (auth == "SUCCESS")
                {
                    IsSuccess = true;
                    //aRedirect.InnerText = "Click here to Login";
                    divMessage.InnerHtml = "<Strong>Success! </strong>Your Transaction is Successfully completed Please click here to login";
                    divMessage.Attributes.Add("class", "alert alert-success");
                }
                else
                {
                    // aRedirect.InnerText = "Registrater Me..";
                    string s = "<b>Sorry ! </b></br>";
                    s = s + "<div style='margin-top:30px;'>We have encountered a problem while processing your payment request.pls try again,.</div>";
                    s = s + "<div style='margin-top:5px;'>Please click here to go back to <a href='merchantlogin.aspx'>REGISTRATION/LOGIN.</a></div>";
                    divMessage.InnerHtml = s;
                }
                //update History
                SubscriptionHistory subHistory = new SubscriptionHistory(_dataObject);
                subHistory.Id = Id;
                subHistory.IsSuccess = IsSuccess;
                subHistory.TransactionId = transactionId;
                subHistory.UpdatePaymentStatus();

                subHistory.GetSubscriptionHistoryById();
                if (subHistory.PromotionId > 0)
                {
                    PromotionCode promotionCode = new PromotionCode(_dataObject);
                    promotionCode.Id = Convert.ToInt32(subHistory.PromotionId);
                    promotionCode.UpdateUsedCount();
                }
            }
            else
            {
               

            }
         
        }
        catch (Exception ex)
        {
          //  aRedirect.InnerText = "Click here to Login";
          //  divMessage.InnerHtml = "<Strong>Success! </strong>Your Subscription has been done.We will send Login Information on registered emailId!!";
          //  divMessage.Attributes.Add("class", "alert alert-success");
            string s = "<b>Thank-you</b></br>";
            s = s + "<div style='margin-top:30px;'>Your free subscription activated and your login credential has been email to you,Please check your email account.</div>";
            s = s + "<div style='margin-top:5px;'>Please click here to <a href='merchantlogin.aspx'>LOGIN.</a></div>";
            divMessage.InnerHtml = s;
        }
    }
}